package com.jacky.iot.utils;

import java.security.SecureRandom;
import java.util.Random;

public class ShortUUID {
    private static final String CHARACTERS =
            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final Random random = new SecureRandom();

    /**
     * 生成指定长度的随机字符串
     */
    public static String generate(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
        }
        return sb.toString();
    }

    /**
     * 生成8位短ID（适用于非严格唯一性要求的场景）
     */
    public static String shortUUId() {
        return generate(8);
    }

    /**
     * 生成时间戳+随机数的组合ID
     */
    public static String generateTimestampId() {
        long timestamp = System.currentTimeMillis();
        String randomPart = generate(6);
        return Long.toHexString(timestamp) + randomPart;
    }
}
